/*
*    Phantom Test Driver v2.0 GUI Automation Software
*    Copyright (C) 2009  John C. Van Arsdall (john@phantomtest.com)
*
*    This program is free software; you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation; either version 2 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License along
*    with this program; if not, write to the Free Software Foundation, Inc.,
*    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*//*
 * SSI_SockCmd.java
 *
 * Created on October 1, 2005, 8:42 PM
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

/**
 *
 * @author John
 */

package ssi_socket;

import java.io.*;
import ptd.Main;

public class SSI_SockCmd {
    
    /** Creates a new instance of SSI_SockCmd */
    public SSI_SockCmd() {
    }
    
    public String Data;
    public int Size;
    public int ID;
    public int ConnectionID;
    public int SequenceCtr;
    
    public boolean bValidSequence;
    
    public void Set(int inID, int inSize, String inData){
        Data = inData;
        Size = inSize;
        ID = inID;
    }

    public int sendMe(SSI_Socket theSocket) {
        
        byte[] sData;
        if(Data != null){
            sData = Data.getBytes();
        }else{
            sData = new byte[1];
            sData[0] = 0;
        }
        
        try{
            
            
            // Write command ID
            theSocket.writeInt(ID);
            
            // Write connection ID
            theSocket.writeInt(ConnectionID);

            // Write sequence counter            
            theSocket.writeInt(SequenceCtr);

            // Write Size
            theSocket.writeInt(Size);
            //Main.debug_out(theSocket.MySocket.getPort());
            //Main.debug_out(Size);
            
            if(Size > 0){
                theSocket.out.write(sData, 0, Size);
            }
            
            theSocket.out.flush();
            
            SequenceCtr = SequenceCtr + 1;
        }catch(Exception e){
            Main.debug_out(e.getMessage());
            theSocket.lastError = e.getMessage();
            return 0;
        }
        return 1;
    }

    public int readMe(SSI_Socket theSocket) {

        int RSP_ERROR = 0;
        int OldCtr = SequenceCtr;
        
        try{
            byte[] bData = new byte[4];
            
            // Read the Command ID
            //theSocket.in.read()
            if(theSocket.in.read(bData, 0, 4) != 4){
                theSocket.lastError = "SSI_Socket: Read data differs from expected size (4)";
                ID = RSP_ERROR;
                return 0;
            }
            ID = theSocket.IntFromBytes(bData);
            
            // Read the connection ID
            if(theSocket.in.read(bData, 0, 4) != 4){
                theSocket.lastError = "SSI_Socket: Read data differs from expected size (4)";
                ID = RSP_ERROR;
                return 0;
            }
            ConnectionID = theSocket.IntFromBytes(bData);

            // Read the sequence counter
            if(theSocket.in.read(bData, 0, 4) != 4){
                theSocket.lastError = "SSI_Socket: Read data differs from expected size (4)";
                ID = RSP_ERROR;
                return 0;
            }
            SequenceCtr = theSocket.IntFromBytes(bData);

            if(theSocket.in.read(bData, 0, 4) != 4){
                theSocket.lastError = "SSI_Socket: Read data differs from expected size (4)";
                ID = RSP_ERROR;
                return 0;
            }
            Size = theSocket.IntFromBytes(bData);

            // Read the Command String
            bData = new byte[Size];
            if(theSocket.in.read(bData, 0, Size) != Size){
                theSocket.lastError = "SSI_Socket: Read data differs from expected size";
                ID = RSP_ERROR;
                return 0;
            }
            Data = new String(bData);

            //theSocket.in.
            
            if(SequenceCtr != OldCtr){
                bValidSequence = false;
            }else{
                bValidSequence = true;
            }
            SequenceCtr = SequenceCtr + 1;
                        
        }catch(IOException e){
            theSocket.lastError = e.getMessage();
            ID = RSP_ERROR;
            return 0;
        }
        
        return 1;
    }
}
